﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class administrator_uc_admin_m_leftmenugroup : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["groupid"]))
            {
                LoadDataToDropDownListCategory(DropdownLeftMenuGroup);
                LoadDataInfo(Request.QueryString["groupid"].ToString());                              
                ViewDataGridDS();
                LoadgvDSChonByGroupID(new Guid(hdItemID.Value), 0);
            }                       
        }
    }
    private void LoadDataToDropDownListCategory(DropDownList cboName)
    {
        DataTable dtb = new CmsBLL().Groups_GetList("","");
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "GroupName", "GroupID");
        }
    }
    private void ViewDataGridDS()
    {
        DataTable dt = new CmsBLL().LeftMenu_GetList(0);
        grvDanhSach.DataSource = dt;
        grvDanhSach.DataBind();
    }
    private void LoadDataInfo(string GroupID)
    {
        hdItemID.Value = GroupID;
        DropdownLeftMenuGroup.SelectedValue = GroupID + "";
        //ltGroupName.Text = new CmsBLL().Groups_GetGroupName(new Guid(GroupID));
    }
    private void LoadgvDSChonByGroupID(Guid GroupID, int MenuID)
    {
        grvDanhSachChon.DataSource = null;
        grvDanhSachChon.DataBind();
        DataTable dt = new CmsBLL().LeftMenuGroup_Search(GroupID, MenuID);
        if (dt.Rows.Count > 0)
        {
            grvDanhSachChon.DataSource = dt;
            grvDanhSachChon.DataBind();
        }       
    }   
    protected void btnAddMenu_Click(object sender, EventArgs e)
    {
        if (grvDanhSach.Rows.Count > 0)
        {
            List<int> listid = new List<int>();
            List<int> retlistid = new List<int>();
            for (int i = 0; i < grvDanhSach.Rows.Count; i++)
            {
                CheckBox chkChon = (CheckBox)grvDanhSach.Rows[i].FindControl("chkCheckgvDS");
                HiddenField hdFieldID = (HiddenField)grvDanhSach.Rows[i].FindControl("hdItemIDgvDS");

                if (chkChon.Checked == true)
                {
                    listid.Add(int.Parse("0" + hdFieldID.Value));
                }
            }
            if (listid.Count == 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa chọn menu nào để thêm');", true);
            else
                for (int i = listid.Count - 1; i >= 0; i--)
                {
                    if (int.Parse("0" + new CmsBLL().LeftMenuGroup_CheckValueToInsert(new Guid(hdItemID.Value), listid[i])) > 0)
                    {
                        retlistid.Add(listid[i]);
                    }
                    else
                    {
                        try
                        {
                            new BLL.CmsBLL().LeftMenuGroup_InsertItem(new Guid(hdItemID.Value), listid[i]);
                        }
                        catch (Exception)
                        {
                            retlistid.Add(listid[i]);
                        }
                    }
                }
            if (retlistid.Count > 0)
            {
                string strvalue = "Các items không được thêm ở danh sách chọn: ";
                for (int i = 0; i < retlistid.Count; i++)
                {
                    strvalue += "  -" + new BLL.CmsBLL().LeftMenu_GetTitle(retlistid[i]);
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + strvalue + "');", true);
            }
        }
        LoadgvDSChonByGroupID(new Guid(hdItemID.Value), 0);
    }
    protected void btnRemoveMenu_Click(object sender, EventArgs e)
    {
        if (grvDanhSachChon.Rows.Count > 0)
        {
            List<int> listid = new List<int>();
            List<int> retlistid = new List<int>();
            for (int i = 0; i < grvDanhSachChon.Rows.Count; i++)
            {
                CheckBox chkChon = (CheckBox)grvDanhSachChon.Rows[i].FindControl("chkCheckgvDSChon");
                HiddenField hdFieldID = (HiddenField)grvDanhSachChon.Rows[i].FindControl("hdItemIDgvDSChon");

                if (chkChon.Checked == true)
                {
                    listid.Add(int.Parse("0" + hdFieldID.Value));
                }
            }
            if (listid.Count == 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn chưa chọn menu nào để xóa');", true);
            else
                for (int i = listid.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        new BLL.CmsBLL().LeftMenuGroup_DeleteItem(new Guid(hdItemID.Value), listid[i]);
                    }
                    catch (Exception)
                    {
                        retlistid.Add(listid[i]);
                    }
                }
            if (retlistid.Count > 0)
            {
                string strvalue = "Các items không được xóa khỏi danh sách chọn: ";
                for (int i = 0; i < retlistid.Count; i++)
                {
                    strvalue += "  -" + new BLL.CmsBLL().LeftMenu_GetTitle(retlistid[i]);
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + strvalue + "');", true);
            }
        }
        LoadgvDSChonByGroupID(new Guid(hdItemID.Value), 0);
    }
}